package com.jlf.starter.logger.reactive;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jlf.starter.logger.reactive.entity.LogObject;
import com.jlf.tool.clock.SystemClock;
import com.jlf.tool.gson.adapter.LocalDateAdapter;
import com.jlf.tool.gson.adapter.LocalDateTimeAdapter;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 接口日志记录器
 *
 * @author wujr
 * 2023/8/3
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/8/3 1.0 新增]
 */
@Slf4j
public class ApiLogger {
    private static final Gson gson = new GsonBuilder()
//            .disableHtmlEscaping()
//            .setPrettyPrinting()
            .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter())
            .registerTypeAdapter(LocalDate.class, new LocalDateAdapter())
            .create();
    public static void log(LogObject record){
        record.setResponseTime(LocalDateTime.now());
        record.setTimeElapsed(SystemClock.now() - record.getRequestMillis());
        log.info(gson.toJson(record));
    }
}
